﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;

namespace VSM.Business.Purchasing
{
    public partial class MRPPOList : System.Web.UI.Page
    {
        private static string FromDocDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("dd/MM/yyyy");
        private static string ToDocDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month)).ToString("dd/MM/yyyy");

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                this.Page.Title = "Order Recommendation";
                if (Session[Utils.AppConstants.ID_User] == null
                    || Session[Utils.AppConstants.Password] == null
                    || Session[Utils.AppConstants.ID_Dealer] == null)
                {
                    Response.Redirect("Login.aspx");
                }
                else
                {
                    if (!IsPostBack)
                    {
                        txtFromDocDate.Text = FromDocDate;
                        txtToDocDate.Text = ToDocDate;
                        LoadScenarioList();
                        BindDocList(new DataTable());
                    }
                }
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + ex.ToString() + "','');", true);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            Timer1.Enabled = false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdBP_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                GridViewRow row = this.grdBP.SelectedRow;
                DataTable dt = (DataTable)Session["DocMRPPOListTable"];
                if (dt != null)
                {
                    int index = row.DataItemIndex;
                    var data = dt.Rows[index];
                    if (data != null)
                    {
                        if (data["Status"].ToString() != "C")
                        {
                            ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('/Business/Purchasing/CreatePO.aspx?MRPPO=1&MRPPOID=" + data["Id"].ToString() + "');", true);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + ex.ToString() + "','');", true);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdBP_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");
                    e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)"); 
                    e.Row.Attributes["style"] = "cursor:pointer";
                    e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grdBP, "Select$" + e.Row.RowIndex);
                }
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + ex.ToString() + "','');", true);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            FromDocDate = txtFromDocDate.Text;
            ToDocDate = txtToDocDate.Text;
            LoadData();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAccept_Click(object sender, EventArgs e)
        {
            try
            {
                GridViewRow selectedRow = this.grdBP.SelectedRow;
                if (selectedRow == null) return;
                Session["ChosenDocList"] = selectedRow;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "OK", "Main.okDialogClick('SelectDocList');", true);
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + ex.ToString() + "','');", true);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private void LoadData()
        {
            try
            {
                MasterData obj = new MasterData();
                string dbType = Session[Utils.AppConstants.ID_Dealer].ToString() == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;

                DateTime fromDocDate = Utils.AppConstants.ConvertToDate((string.IsNullOrEmpty(txtFromDocDate.Text) ? "01/01/1900" : txtFromDocDate.Text));
                DateTime toDocDate = Utils.AppConstants.ConvertToDate((string.IsNullOrEmpty(txtToDocDate.Text) ? "31/12/9998" : txtToDocDate.Text));
                DateTime fromDueDate = Utils.AppConstants.ConvertToDate((string.IsNullOrEmpty(txtFromDueDate.Text) ? "01/01/1900" : txtFromDueDate.Text));
                DateTime toDueDate = Utils.AppConstants.ConvertToDate((string.IsNullOrEmpty(txtToDueDate.Text) ? "31/12/9998" : txtToDueDate.Text));

                DataSet dsDocList = obj.GetMRPPOList(dbType, Session[Utils.AppConstants.ID_Dealer].ToString(),
                    "", fromDocDate, toDocDate, fromDueDate, toDueDate,
                    txtFromDocTotal.Text, txtToDocTotal.Text, ddlStatus.SelectedValue, this.ddlScenarioCode.SelectedValue.ToString());

                if (dsDocList != null && dsDocList.Tables != null)
                    BindDocList(dsDocList.Tables[0]);
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + ex.ToString() + "','');", true);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private void LoadScenarioList()
        {
            try
            {
                string dbType = Session[Utils.AppConstants.ID_Dealer].ToString() == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;
                MasterData obj = new MasterData();
                DataSet ds = obj.GetScenario(dbType, Session[Utils.AppConstants.ID_Dealer].ToString());

                if (ds != null)
                {
                    this.ddlScenarioCode.DataSource = ds;
                    this.ddlScenarioCode.DataTextField = "ScenarioName";
                    this.ddlScenarioCode.DataValueField = "ScenarioName";
                    this.ddlScenarioCode.DataBind();
                }
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + ex.ToString() + "','');", true);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tb"></param>
        private void BindDocList(DataTable tb)
        {
            Session["DocMRPPOListTable"] = tb;
            this.grdBP.DataSource = tb;
            this.grdBP.DataBind();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCreatePO_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)Session["DocMRPPOListTable"];
            if (dt != null && dt.Rows.Count>0)
            {
                var data = dt.Rows[0];
                if (data != null)
                {
                    if (data["Status"].ToString() != "C")
                    {
                        ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('/Business/Purchasing/CreatePO.aspx?MRPPO=1&MRPPOID=" + data["Id"].ToString() + "');", true);
                    }
                }
            }
        }
    }
}